package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.UserCenterService;
import com.zerdoor.util.Email;
import com.zerdoor.util.MD5;
import com.zerdoor.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: acg
 * Module ID: 005
 * Comments: 会员中心
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaoji
 * Create Date：2014-10-27
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("/usercenter")
public class UserCenterController {
    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    private Email simpleMail;
    //发送邮箱对象
    @Value("#{mailsenderProperties['mail.from']}")
    private String fromMail;
    /**
     * FunName:           index
     * Description :      会员中心-初始页面
     * @return String：初始页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model,HttpServletRequest request){
        //是否竞标
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //收入
        model.addAttribute("income", userCenterService.getUserIncome(userInfo.getUserId()));
        //支出
        model.addAttribute("payout", userCenterService.getUserPayout(userInfo.getUserId()));
        //余额
        model.addAttribute("wallet", userCenterService.getUserWallet(userInfo.getUserId()));

        //未读信息
        model.addAttribute("messTotal",userCenterService.getTotalMessages(userInfo.getUserId()));

        model.addAttribute("toUrl",request.getParameter("toUrl"));
        initindex(model,request);
        return "/usercenter/index";
    }
    @RequestMapping(value = "/getCenterInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getCenterInfo(HttpServletRequest request){
        Map<String,Object> map = new HashMap();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //收入
        map.put("income", userCenterService.getUserIncome(userInfo.getUserId()));
        //支出
        map.put("payout", userCenterService.getUserPayout(userInfo.getUserId()));
        //余额
        map.put("wallet", userCenterService.getUserWallet(userInfo.getUserId()));

        //未读信息
        map.put("messTotal",userCenterService.getTotalMessages(userInfo.getUserId()));
        return map;
    }

    /**
     * FunName:           index
     * Description :      会员中心-初始页面
     * @return String：初始页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/initindex", method = RequestMethod.GET)
    public String initindex(Model model,HttpServletRequest request){
        //是否竞标
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //近期投标任务
        model.addAttribute("rtasks", userCenterService.getUserReceiverTaskInfo(userInfo.getArtistId()));
        //近期招标任务
        model.addAttribute("stasks", userCenterService.getUserSenderTaskInfo(userInfo.getUserId()));
        //新闻公告
        model.addAttribute("news", userCenterService.getArtArticleFornews());
        return "/usercenter/initindex";
    }

    /**
     * FunName:           taskManager
     * Description :      会员中心-艺术家-招投标管理
     * @return String：返回招投标页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getItemInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getItemInfo(HttpServletRequest request,int page,int rows,int type){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list= null;
        if(type == 1){//进行中的项目
            list = userCenterService.getArtistGoingTasks(userInfo.getArtistId());
        }else if(type == 2){//近期投标项目
            list = userCenterService.getArtistReceiverTasks(userInfo.getArtistId());
        }else if(type==3){//历史参与项目
            list = userCenterService.getArtistHistoryTasks(userInfo.getArtistId());
        }
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);

        return map;
    }
    @RequestMapping(value = "/toTaskManager", method = RequestMethod.GET)
    public String toTaskManager(int type,Model model){
        model.addAttribute("type",type);
        return "/usercenter/taskManager";
    }
    /**
     * FunName:           proManager
     * Description :      会员中心-艺术家-作品管理
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/proManager", method = RequestMethod.GET)
    public String proManager(Model model,HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        //作品管理
        model.addAttribute("photos", userCenterService.getArtistPhotoes(userInfo.getArtistId()));
        return "/usercenter/proManager";
    }

    /**
     * FunName:           proManager
     * Description :      会员中心-艺术家-作品订单分页管理
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getOrderWorks", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getOrderWorks(Integer page,Integer rows,
                                          HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        //作品出售订单
        List<Map<String, Object>> list = userCenterService.getArtistProOrder(userInfo.getUserId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return  map;
    }

    /**
     * FunName:           addPhoto
     * Description :      会员中心-艺术家-添加专辑
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/addPhoto", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addPhoto(HttpServletRequest request,String photoName){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("artistId", userInfo.getArtistId());
        map.put("photoName", photoName);
        map.put("photo_id", null);
        userCenterService.addPhoto(map);
        map.put("status",1);
        return map;
    }

    /**
     * FunName:           updatePhoto
     * Description :      会员中心-艺术家-编辑专辑
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/updatePhoto", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updatePhoto(HttpServletRequest request,Integer photoId,String photoName){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("artistId", userInfo.getArtistId());
        map.put("photoName", photoName);
        map.put("photoId", photoId);
        userCenterService.updatePhoto(map);
        map.put("status",1);
        return map;
    }

    /**
     * FunName:           updatePhoto
     * Description :      会员中心-艺术家-删除专辑
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/deletePhoto", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deletePhoto(HttpServletRequest request,Integer photoId){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("artistId", userInfo.getArtistId());
        map.put("photoId", photoId);
        try {
            userCenterService.deletePhoto(map);
            map.put("status",1);
        }catch (Exception e){
            map.put("status",-1);
        }

        return map;
    }

    /**
     * FunName:           getArtistProByPhotoId
     * Description :      会员中心-艺术家-作品管理
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getArtistProByPhotoId", method = RequestMethod.GET)
    public String getArtistProByPhotoId(HttpServletRequest request,Integer photoId,Model model){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("photoId", photoId);
        model.addAttribute("photoId",photoId);
        return "/usercenter/productions";
    }

    /**
     * FunName:           getArtistWorksByPhotoId
     * Description :      会员中心-艺术家-作品管理
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-12-5
     */
    @RequestMapping(value = "/getArtistWorksByPhotoId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getArtistWorksByPhotoId(HttpServletRequest request,Integer photoId,
                                                       Integer page, Integer rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("artistId", userInfo.getArtistId());
        map.put("photoId", photoId);
        PageHelper.startPage(page, rows);
        //作品出售订单
        List<Map<String, Object>> list = userCenterService.getArtistProByPhotoId(map);
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           toPro
     * Description :      会员中心-艺术家-跳转到添加作品页面
     * @return String：添加作品页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/toPro", method = RequestMethod.GET)
    public String toPro(Integer photoId,Model model){
        model.addAttribute("photoId",photoId);
        return "/usercenter/proAdd";
    }

    /**
     * FunName:           addProduct
     * Description :      会员中心-艺术家-添加作品
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
     @ResponseBody
     public Map<String, Object> addProduct(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("photoId", request.getParameter("photoId"));
        map.put("name", request.getParameter("proName"));
        map.put("waterFileurl", request.getParameter("officialFileurl"));
        if(request.getParameter("officialFileurl") != null){
            String[] str=request.getParameter("officialFileurl").toString().split(",");
            String cover = str.length>0?str[0]:"";
            map.put("cover", cover);
        }
        map.put("officialFileurl", request.getParameter("officialFileurl"));
        map.put("worksStatus", request.getParameter("works_status"));
        String price = request.getParameter("price");
        if("".equals(price)){
            price=null;
        }
        map.put("price", price);
        map.put("firstClass", request.getParameter("firstClass"));
        map.put("secondClass", request.getParameter("secondClass"));
        map.put("description", request.getParameter("description"));

        userCenterService.addArtistPro(map);
        map.put("status",1);
        return map;
    }

    /**
     * FunName:           toProEdit
     * Description :      会员中心-艺术家-跳转到编辑作品页面
     * @return String：编辑作品页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/toProEdit", method = RequestMethod.GET)
    public String toProEdit(Integer productionId,Model model,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("artistId",userInfo.getArtistId());
        map.put("worksId",productionId);

        model.addAttribute("pro",userCenterService.initUpdateArtistPro(map));
        return "/usercenter/proEdit";
    }

    /**
     * FunName:           deleteProduct
     * Description :      会员中心-艺术家-删除作品
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-13
     */
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteProduct(HttpServletRequest request,Integer productionId){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        try {
            map.put("artistId",userInfo.getArtistId());
            map.put("productionId",productionId);
            userCenterService.deletePro(map);
            map.put("status",1);
        }catch (Exception e){
            map.put("status",-1);
        }
        return map;
    }

    /**
     * FunName:           updateProduct
     * Description :      会员中心-艺术家-编辑作品
     * @return String：作品管理页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateProduct(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("productionId", request.getParameter("productionId"));
        map.put("name", request.getParameter("proName"));
        map.put("waterFileurl", request.getParameter("officialFileurl").toString());
        map.put("officialFileurl", request.getParameter("officialFileurl").toString());
        if(request.getParameter("officialFileurl") != null){
            String[] str=request.getParameter("officialFileurl").toString().split(",");
            String cover = str.length>0?str[0]:"";
            map.put("cover", cover);
        }
        map.put("works_status", request.getParameter("works_status"));
        String price = request.getParameter("price");
        if("".equals(price)){
            price=null;
        }
        map.put("price", price);
//        map.put("price", request.getParameter("price"));
        map.put("firstClass", request.getParameter("firstClass"));
        map.put("secondClass", request.getParameter("secondClass"));
        map.put("description", request.getParameter("description"));
        map.put("artistId", userInfo.getArtistId());

        try {
            userCenterService.updateArtistPro(map);
            map.put("status", 1);
        } catch (Exception e) {
            map.put("status", -1);
        }
        return map;
    }

    /**
     * FunName:           getArtistInfo
     * Description :      会员中心-艺术家-介绍资料
     * @return String：艺术家介绍页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getArtistInfo", method = RequestMethod.GET)
    public String getArtistInfo(HttpServletRequest request, Model model){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        Map<String ,Object> map = userCenterService.getArtistInfo(userInfo.getArtistId());
        model.addAttribute("artistInfo", map);
        //艺术家服务
        model.addAttribute("service", userCenterService.getSecondBaseTasktype());
        //艺术家技能
        model.addAttribute("skills", userCenterService.getArtistSkill());
        return "/usercenter/artistInfo";
    }

    /**
     * FunName:           updateUserArtist
     * Description :      会员中心-艺术家-更新介绍资料
     * @return String：艺术家介绍页面
     * @Author:       xiaoji
     * @Create Date: 2014-12-8
     */
    @RequestMapping(value = "/updateUserArtist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateUserArtist(HttpServletRequest request, Model model){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("artistId", userInfo.getArtistId());
        map.put("service", request.getParameter("service"));
        map.put("skill", request.getParameter("skill"));
        map.put("intro_simplify", request.getParameter("intro_simplify"));
        map.put("intro_complete", request.getParameter("intro_complete"));
        try {
            userCenterService.updateUserArtist(map);
            map.put("status","1");
        } catch (Exception e) {
            map.put("status","-1");
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "/toBidTaskManager", method = RequestMethod.GET)
    public String toBidTaskManager(int type,Model model){
        model.addAttribute("type",type);
        return "/usercenter/bidTaskManager";
    }

    /**
     * FunName:           bidTaskManager
     * Description :      会员中心-委托方-招投标管理
     * @return String：返回招投标页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/bidTaskManager", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> bidTaskManager(HttpServletRequest request,int page,int rows,int type){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list= null;
        if(type == 1){//进行中的项目
            list = userCenterService.getPrincipalGoingTasks(userInfo.getUserId());
        }else if(type == 2){//近期投标项目
            list = userCenterService.getPrincipalBidTasks(userInfo.getUserId());
        }else if(type==3){//历史参与项目
            list = userCenterService.getPrincipalHistoryBidTasks(userInfo.getUserId());
        }
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           getSingleUserInfoByUserId
     * Description :      会员中心-账户信息
     * @return String：账户信息认证页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/topup", method = RequestMethod.GET)
    public String topup(Model model, HttpServletRequest request){
        return "/usercenter/topup";
    }

    /**
     * 充值日志
     * @param model
     * @param request
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/topupList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> topupList(Model model, HttpServletRequest request,int page,int rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = userCenterService.getPayInfo(userInfo.getUserId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           getSingleUserInfoByUserId
     * Description :      会员中心-账户信息
     * @return String：账户信息认证页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/withdrawal", method = RequestMethod.GET)
    public String withdrawal(Model model, HttpServletRequest request){
        return "/usercenter/withdrawal";
    }

    /**
     * 提现日志
     * @param model
     * @param request
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/withdrawalList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> withdrawalList(Model model, HttpServletRequest request,int page,int rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        //提现信息
        List<Map<String, Object>> list = userCenterService.getPayOutInfo(userInfo.getUserId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           getSingleUserInfoByUserId
     * Description :      会员中心-站内消息
     * @return String：站内消息页面
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getMessagesById", method = RequestMethod.GET)
    public String getMessagesById(Model model, HttpServletRequest request){
        return "/usercenter/userMessages";
    }
    @RequestMapping(value = "/getMessages", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMessages(Model model, HttpServletRequest request,Integer page,Integer rows){
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        List<Map<String, Object>> list = userCenterService.getMessagesById(userInfo.getUserId());
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }
    /**
     * FunName:           userMessagesHome
     * Description :      会员中心-站内消息房间
     * @return String：站内消息房间
     * @Author:       xiaoji
     * @Create Date: 2014-12-16
     */
    @RequestMapping(value = "/toUserMessagesHome", method = RequestMethod.GET)
    public String toUserMessagesHome(Model model, HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        model.addAttribute("sender",request.getParameter("sender"));
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("sender",request.getParameter("sender"));
        map.put("receiver",userInfo.getUserId());
        try {
            model.addAttribute("readNum",userCenterService.updateUserMessageRead(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/usercenter/userMessagesHome";
    }

    /**
     * FunName:           getMessagesByReceiver
     * Description :      会员中心-站内消息房间消息
     * @return String：站内消息房间消息
     * @Author:       xiaoji
     * @Create Date: 2014-11-11
     */
    @RequestMapping(value = "/getMessagesByReceiver", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMessagesByReceiver(Model model, HttpServletRequest request,Integer page,Integer rows){
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("userId", userInfo.getUserId());
        map.put("sender", request.getParameter("sender"));

        List<Map<String, Object>> list = userCenterService.getMessagesByReceiver(map);
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return map;
    }

    /**
     * FunName:           addUserMessage
     * Description :      会员中心-添加消息
     * @return String：添加消息结果
     * @Author:       xiaoji
     * @Create Date: 2014-12-16
     */
    @RequestMapping(value = "/addUserMessage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addUserMessage(HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sender",userInfo.getUserId());
        map.put("receiver",request.getParameter("receiver"));
        map.put("theme","");
        map.put("content",request.getParameter("content"));
        try {
            userCenterService.addUserMessage(map);
            map.put("status",0);
        } catch (Exception e) {
            map.put("status",-1);
            e.printStackTrace();
        }
        return map;
    }


    /**
     * FunName:           getUserBaile
     * Description :      会员中心-委托方-介绍资料
     * @return String：介绍资料
     * @Author:       xiaoji
     * @Create Date: 2014-12-15
     */
    @RequestMapping(value = "/getUserBaile", method = RequestMethod.GET)
    public String getUserBaile(Model model,HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String,Object> map = userCenterService.getUserBaileInfo(userInfo.getBaileId());
        model.addAttribute("intro",map.get("intro"));
        return "/usercenter/userBaile";
    }

    /**
     * 修改委托方资料
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateUserBaileInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserBaileInfo (HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("intro", request.getParameter("intro"));
        map.put("baileId",userInfo.getBaileId());
        try {
            userCenterService.updateUserBaileInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","更新委托方资料失败");
        }
        return map;
    }

    /**
     * FunName:           collection
     * Description :      会员中心-收藏管理
     * @return String：收藏管理
     * @Author:       xiaoji
     * @Create Date: 2014-12-15
     */
    @RequestMapping(value = "/collection", method = RequestMethod.GET)
    public String collection(Model model,int type){
        model.addAttribute("type",type);
        return "/usercenter/collection";
    }

    @RequestMapping(value = "/getCollectionList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCollectionList(HttpServletRequest request,int type,int page,int rows){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String,Object> map = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = null;
        if(type == 1){
            list = userCenterService.colloctionProction(userInfo.getUserId());
        }else if(type == 2){
            list = userCenterService.colloctionArtist(userInfo.getUserId());
        }else if(type == 3){
            list = userCenterService.colloctionBaile(userInfo.getUserId());
        }else if(type == 4){
            list = userCenterService.colloctionProject(userInfo.getUserId());
        }
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        map.put("type",type);
        return map;
    }

    @RequestMapping(value = "/deleteCollection", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteCollection(HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            userCenterService.deleteColloction(Integer.parseInt(request.getParameter("favoriteId")) ,userInfo.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","删除失败！");
        }

        return map;
    }

    /**
     * FunName:           getMessagesByReceiver
     * Description :      会员中心-账户信息
     * @return String：站内消息房间
     * @Author:       xiaoji
     * @Create Date: 2014-12-15
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public String getUserInfo(){
        return "/usercenter/userInfo";
    }

    /**
     * FunName:           promote
     * Description :      会员中心-推广管理
     * @return String：站内消息房间
     * @Author:       xiaoji
     * @Create Date: 2014-12-15
     */
    @RequestMapping(value = "/promote", method = RequestMethod.GET)
    public String promote(Model model,HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        model.addAttribute("total",userCenterService.getTotalPresenter(userInfo.getUserId()).get("total"));
        return "/usercenter/promote";
    }

    /**
     * 修改密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param repeatPassword 确认密码
     * @param request 请求
     * @return 返回修改密码结果
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updatePassword(String oldPassword,String newPassword,
                                             String repeatPassword,HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        if("".equals(oldPassword)){
            map.put("errormsg","请输入旧密码！");
            return map;
        }
        if("".equals(newPassword)){
            map.put("errormsg","请输入新密码！");
            return map;
        }
        if("".equals(repeatPassword) || !repeatPassword.equals(newPassword)){
            map.put("errormsg","两次输入密码不一致，请重新输入！");
            return map;
        }
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        if(!userInfo.getPsd().equals(MD5.getMd5String(oldPassword))){
            map.put("errormsg","密码不正确，请重新输入！");
            return map;
        }
        try {

            map.put("password", MD5.getMd5String(newPassword));
            map.put("userId", userInfo.getUserId());
            userCenterService.updatePassword(map);
            userInfo.setPsd(MD5.getMd5String(newPassword));
            //重载登陆会话
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","更新密码失败！");
        }
        return map;
    }

    /**
     * 更改头像
     * @param request
     * @param attachment4 头像
     * @return
     */
    @RequestMapping(value = "/updateUserHeadPortrait", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserHeadPortrait(HttpServletRequest request,String attachment4){
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("headPortrait", attachment4);
        map.put("userId", userInfo.getUserId());
        try {
            userCenterService.updateUserHeadPortrait(map);
            userInfo.setHeadPortrait(attachment4);
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","更改头像失败！");
        }
        return map;
    }

    /**
     * 更改真实姓名，身份证，资质
     * @param request
     * @param attachment3 资质证书
     * @param realName 头像
     * @param attachment1 身份证
     * @param attachment2 营业执照
     * @return
     */
    @RequestMapping(value = "/updateUserRealName", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserRealName(HttpServletRequest request,String attachment3, String realName,
                                                 String attachment1, String attachment2){
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("realName", realName);
        map.put("idCard", attachment1);
        map.put("businessLicense", attachment2);
        map.put("cer", attachment3);
        map.put("userId", userInfo.getUserId());
        try {
            userCenterService.updateUserRealName(map);
            userInfo.setRealName(realName);
            userInfo.setIdCard(attachment1);
            userInfo.setBusinessLicense(attachment2);
            userInfo.setCer(attachment3);
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","认证失败！");
        }
        return map;
    }

    /**
     * 更改email
     * @param request
     * @param email 邮箱
     * @return
     */
    @RequestMapping(value = "/updateUserEmail", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserEmail(HttpServletRequest request,String email,String code){
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("userId", userInfo.getUserId());
        map.put("email", email);
        try {
            int total = userCenterService.checkUserEmail(map);
            if(total > 0) {
                map.put("errormsg","邮箱已被使用！");
                return map;
            }
            String authCode = (String)request.getSession().getAttribute("authCode");
            if(authCode == null){
                map.put("errormsg","未发送链接地址！");
                return map;
            }
            if(!authCode.equals(code)){
                map.put("errormsg","认证失败，请重新发送链接地址！");
                return map;
            }
            userCenterService.updateUserEmail(map);
            userInfo.setEmail(email);
            request.getSession().setAttribute("curUser",userInfo);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","修改邮箱失败！");
        }
        return map;
    }

    /**
     * 发送邮件链接地址
     * @param request
     */
    @RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
    @ResponseBody
    public String sendEmail(HttpServletRequest request,String email){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        String randomStr = RandomUtil.generateString(6);
        request.getSession().setAttribute("authCode",randomStr);
        //发邮件
        simpleMail.sendMail("邮箱认证","acg平台邮箱认证动态验证码："+randomStr,fromMail,email);
        return "{}";
    }

    /**
     * 更改手机号码
     * @param request
     * @param phone 邮箱
     * @return
     */
    @RequestMapping(value = "/updateUserPhone", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserPhone(HttpServletRequest request,String phone){
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("userId", userInfo.getUserId());
        map.put("phone", phone);
        try {
            int total = userCenterService.checkUserPhone(map);
            if(total > 0) {
                map.put("errormsg","手机号码已被使用！");
                return map;
            }
            userCenterService.updateUserPhone(map);
            userInfo.setPhone(phone);
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","修改手机号码失败！");
        }
        return map;
    }

    /**
     * 更改手机号码
     * @param request
     * @param userqq 邮箱
     * @return
     */
    @RequestMapping(value = "/updateUserqq", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUserqq(HttpServletRequest request,String userqq){
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        map.put("userId", userInfo.getUserId());
        map.put("userqq", userqq);
        try {
            userCenterService.updateUserqq(map);
            userInfo.setQq(userqq);
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errormsg","保存用户qq失败！");
        }
        return map;
    }

    /**
     * 用户更改手机号码验证
     * @param phone 手机号码
     * @return
     */
    @RequestMapping(value = "/checkUserPhone", method = RequestMethod.GET)
    @ResponseBody
   public  boolean checkUserPhone(String phone,HttpServletRequest request){
       Map<String,Object> map = new HashMap<String, Object>();
       UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
       map.put("userId",userInfo.getUserId());
       map.put("phone",phone);
       int total = userCenterService.checkUserPhone(map);
       if(total>0) return false;
       return true;
   }

    /**
     * 用户更改邮箱验证
     * @param email 用户邮箱
     * @return
     */
    @RequestMapping(value = "/checkUserEmail", method = RequestMethod.GET)
    @ResponseBody
   public  boolean checkUserEmail(String email,HttpServletRequest request){
       Map<String,Object> map = new HashMap<String, Object>();
       UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
       map.put("userId",userInfo.getUserId());
       map.put("email",email);
       int total = userCenterService.checkUserEmail(map);
       if(total>0) return false;
       return true;
   }

    /**
     * 提现申请操作
     */
    @RequestMapping(value = "withdrawalApplay", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> withdrawalApplay(HttpServletRequest request,String payOutPrice){
        Map<String,Object> map = new HashMap<String, Object>();
        //调用提现申请接口，保存申请记录，并且冻结提现资金
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        try {
            if(userInfo.getAlipay() == null || "".equals(userInfo.getAlipay())){
                map.put("errormsg","支付宝为空，请到账户信息中认证支付宝。");
                return map;
            }
            if(userInfo.getRealName() == null || "".equals(userInfo.getRealName())){
                map.put("errormsg","真实姓名为空，请到账户信息中认证真实姓名。");
                return map;
            }
            BigDecimal money = userCenterService.addUserWithdraw(userInfo.getUserId(),new BigDecimal(payOutPrice));
            userInfo.setFreezeMoney(money);
            request.getSession().setAttribute("curUser",userInfo);
        } catch (Exception e) {
            map.put("errormsg","提现申请失败");
        }
        //payOutPrice
        return map;
    }

    /**
     * 保存支付宝
     */
    @RequestMapping(value = "/authAlipay", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> authAlipay(String alipay,HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        if(alipay !=null && !"".equals(alipay)){
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
            map.put("userId",userInfo.getUserId());
            map.put("alipay",alipay);
            try {
                userCenterService.updateAlipay(map);
                userInfo.setAlipay(alipay);
                request.getSession().setAttribute("curUser", userInfo);
            } catch (Exception e) {
                map.put("errormsg", "支付宝保存失败！");
            }
            //保存支付宝账号
            //加载用户SESSION
        }else{
            map.put("errormsg","支付宝账号不能为空！");
        }
        return map;
    }
}
